package com.ziytek.web.citizen.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ziytek.web.citizen.caffeine.PositionCacheConfig;
import com.ziytek.web.citizen.enums.ResponseEnum;
import com.ziytek.web.citizen.mapper.VersionInfoMapper;
import com.ziytek.web.citizen.model.bean.ControllerVersion;
import com.ziytek.web.citizen.model.rsp.VersionRsp;
import com.ziytek.web.citizen.pojo.ProgramVersionInfo;
import com.ziytek.web.citizen.service.itf.ControllerVersionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author fenghx
 * @version 1.0
 * @className ControllerVersionServiceImpl
 * Description:
 * @date 2024/5/23 9:44
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ControllerVersionServiceImpl implements ControllerVersionService {
    private final Gson gson;
    private final VersionInfoMapper versionInfoMapper;
    private final PositionCacheConfig positionCacheConfig;

    @Override
    public VersionRsp getVersionInfo(String cabinetNo) {
        VersionRsp versionRsp = new VersionRsp();
        if (StringUtils.isEmpty(cabinetNo)) {
            versionRsp.setRetCode(ResponseEnum.PARAMETER_ERROR.getCode());
            versionRsp.setRetMsg(ResponseEnum.PARAMETER_ERROR.getDesc());
            return versionRsp;
        }

        ProgramVersionInfo versionInfo = versionInfoMapper.findFirstByCabinetNoOrderByCreateTimeDesc(cabinetNo);
        if (versionInfo == null) {
            versionRsp.setRetCode(ResponseEnum.OBJECT_NOT_EXIST.getCode());
            versionRsp.setRetMsg(ResponseEnum.OBJECT_NOT_EXIST.getDesc());
            return versionRsp;
        }

        versionRsp.setMasterVersion(versionInfo.getMasterVersion());

        String controlVersionMapJson = versionInfo.getControlVersionMapJson();
        Map<String, String> versionMap = gson.fromJson(controlVersionMapJson, new TypeToken<Map<String, String>>() {
        }.getType());

        List<ControllerVersion> controllerVersionList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(versionMap)) {
            for (Map.Entry<String, String> entry : versionMap.entrySet()) {
                int position = positionCacheConfig.getControlPosition(entry.getKey());
                ControllerVersion controllerVersion = new ControllerVersion();
                controllerVersion.setControllerNo(String.valueOf(position));
                controllerVersion.setVersion(entry.getValue());
                controllerVersionList.add(controllerVersion);
            }
        }

        // 排序 按机分控板编号 升序排序
        if (!CollectionUtils.isEmpty(controllerVersionList)) {
            controllerVersionList.sort((o1, o2) -> {
                int p1 = Integer.parseInt(o1.getControllerNo());
                int p2 = Integer.parseInt(o2.getControllerNo());
                return p1 - p2;
            });
        }

        versionRsp.setRetCode(ResponseEnum.SUCCESS.getCode());
        versionRsp.setRetMsg(ResponseEnum.SUCCESS.getDesc());
        versionRsp.setControllerVersionList(controllerVersionList);
        return versionRsp;
    }
}
